<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="(sw,i) in list" :key="i">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in goodsList" :key="index">
								<view class="top">
									<view class="left">
									</view>
									<view class="right">{{ item.deal }}</view>
								</view>
								<view class="item"  >
									<view class="left"><image :src="item.goodsUrl" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ item.title }}</view>
										<view class="type">{{ item.type }}</view>
										<view class="delivery-time">出租时间 {{ item.deliveryTime }}</view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(item.price) }}
											<text class="decimal">.{{ priceDecimal(item.price) }}</text>
										</view>
										<view class="number">x{{ item.number }}天</view>
									</view>
								</view>
								<view class="bottom">
									<view v-show="current===0" class="evaluate btn" @click="cancelOrder(item.id)">取消订单</view>
									<view v-show="current!==0"></view>
									<view class="total">
										共租{{ item.number }}天 合计:
										<text class="total-price">
											￥{{ priceInt(totalPrice(item)) }}.
											<text class="decimal">{{ priceDecimal(totalPrice(item)) }}</text>
										</text>
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2" margin-top="110"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				list: [
					{
						name: '预约中'
					},
					{
						name: '租赁中'
					},
					{
						name: '已完成'
					},
					{
						name: '已取消'
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['nomore','loadmore','loadmore','loadmore'],
				loading : 0,
				page:1,
				totalPage:0
			};
		},
		onShow() {
			this.goodsList = [];
			this.getOrderList(0);
		},
		watch:{
			current(val){
				this.page = 1;
				this.goodsList = [];
				this.getOrderList(val);
			}
		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)){
						val = val.toString();
						return val.substring(val.length-2);
					}else{ return '00';}
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)){
						return parseInt(val);
					}else{
						return val;
					}
				};
			}
		},
		methods: {
			reachBottom() {
				if(this.page<this.totalPage){
					this.page++;
					this.getOrderList(this.current);
				}
			},
			// 获取数据
			getOrderList(idx) {
				let postData = {
					orderType:idx,
					page:this.page,
				}
				this.$u.api.orderList(postData).then(res=>{
					this.totalPage  = res.data.totalPage;
					res.data.items.forEach((item)=>{
						this.goodsList.push(item);
					})
				});
				this.loadStatus.splice(this.current,1,"nomore")
			},
			// 总价
			totalPrice(item) {
				let price = item.price * item.number;
				return price.toFixed(2);
			},
			// tab栏切换
			change(index) {
				this.loadStatus.splice(index+1,1,"loading")
				this.swiperCurrent = index;
			},
			transition({ detail: { dx } }) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({ detail: { current } }) {
				this.loadStatus.splice(current+1,1,"loading")
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			cancelOrder(orderId){
				this.$u.api.cancelOrder({orderId:orderId}).then(res=>{
					if(res.code===1){
						// 取消成功
						this.goodsList = [];
						this.getOrderList(0);
					}
				})
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}
	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;
		.top {
			display: flex;
			justify-content: space-between;
			.left {
				display: flex;
				align-items: center;
				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}
			.right {
				color: $u-type-warning-dark;
			}
		}
		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;
			.left {
				margin-right: 20rpx;
				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}
			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}
				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}
				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}
			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;
				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}
				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;
			.total-price {
				font-size: 32rpx;
			}
		}
		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;
			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}
			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;
		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}
		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}
		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}
	.swiper-box {
		flex: 1;
	}
	.swiper-item {
		height: 100%;
	}
</style>
